<?php

class ajoutMediaValidation extends Controller {


	function index()
	{
		
		$this->load->database();
		$this->load->library('form_validation');
		$this->load->helper('form');
		$this->load->helper('url');
		$this->load->helper('path');
		
		
		$data;
		$ok=false;
		
				
		if(isset($_FILES['fileImage']) && $_FILES['fileImage']['name']!="" ){	
		
			$data['media']= $_FILES['fileImage']['name'];               
			$infosfichier = pathinfo($_FILES['fileImage']['name']);
            $extension_upload = $infosfichier['extension'];
            $extensions_autorisees = array('jpg', 'jpeg', 'gif', 'png','JPG','mp3','avi');
                
			if (in_array($extension_upload, $extensions_autorisees)){
				
					if($_FILES['fileImage']['size'] <= 10000000){
						
						$ok=true;
						move_uploaded_file($_FILES['fileImage']['tmp_name'], set_realpath("./image/media/"). basename($_FILES['fileImage']['name']));
						$this->db->set('chemin',$data['media']);	
						
						if($extension_upload=='jpg' || $extension_upload=='jpeg' || $extension_upload=='gif' || $extension_upload=='png'|| $extension_upload=='JPG')
							$this->db->set('type','image');					
						else if($extension_upload=='mp3') $this->db->set('type','audio');
						else if($extension_upload=='avi') $this->db->set('type','video');
						
						$oeuvre = $this->input->post('selectOeuvre');
						if($oeuvre!=0)  $this->db->set('oeuvre',$oeuvre); 
						if (isset($_COOKIE['login'])) $this->db->set (utilisateur,$_COOKIE['login']);
						$this->db->insert('media'); 
						
						
				    }
					
					else  $data['error']="Le fichier est trop volumineux";	
			}
			
			else  $data['error']="Le type du fichier n'est pas autoris&eacute;";									
				
		}
		
		else $data['error']="Vous n'avez pas rentr&eacute; de fichiers";
			
			
		if($ok) $this->load->view('ajoutMediaValidation',$data);

		else{
			
		$query = $this->db->query("SELECT ID_Oeuvre, nom FROM oeuvre where utilisateur='".$_COOKIE['login']."'");

		foreach ($query->result_array() as $row) $data["oeuvres"][]=array($row['ID_Oeuvre'],$row['nom']);
	
		$this->load->view('ajoutMedia',$data);
			
		}
		
		
		
}	
	
}
?>
